﻿package igz.fleaxy.sound 
{
	import flash.events.TimerEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundMixer;
	import flash.media.SoundTransform;
	import flash.utils.Timer;
	import igz.fleaxy.sound.Channel;
	
	/**
	 * ...
	 * @author Intelygenz | www.intelygenz.com
	 * 
	 * Clase Vacia
	 * 
	 */
	public class SoundManager 
	{
		
		protected var _ChannelArray : Array;
		
		protected var $Mute : Boolean;
		
		public function SoundManager() {
			_ChannelArray = new Array();
			
			$Mute = false;
		}
		
		public function $AddChannel( p_label : String, p_sound : Sound, p_function_onComplete : Function = null ) : void {
			var soundArray : Array = new Array();
			var soundChannel : Channel = new Channel( p_function_onComplete );
			
			soundArray.push( p_sound );
			soundArray.push( soundChannel ) ;
			
			
			_ChannelArray[ p_label ] = soundArray;
		}
		
		public function $PlayChannel( p_label : String, p_progressive : Boolean = false, p_autoloop : Boolean = false ) : void {
			var sound      : Sound        = _ChannelArray[p_label][0] as Sound;
			var channel    : Channel      = _ChannelArray[p_label][1] as Channel;
			var soundTrans : SoundTransform = new SoundTransform(0);
			
			channel.$AutoLoop = p_autoloop;
			
			channel.$Play( sound );
			if ( p_progressive ){
				$SetVolume( p_label, 0 );
				$VolumeToMax( p_label );
			}			
		}
		
		public function $StopChannel( p_label : String, p_progressive : Boolean = false ) : void {
			
			if ( (_ChannelArray[p_label][1] as Channel).$SChannel != null ){
				if ( p_progressive ){
					(_ChannelArray[p_label][1] as Channel).$VolumeToMin();
				} else {
					(_ChannelArray[p_label][1] as Channel).$SChannel.stop();
				}
			}
		}
		
		public function $VolumeToMax(  p_label : String  ) : void {
			(_ChannelArray[p_label][1] as Channel).$VolumeToMax();
		}
		
		public function $VolumeToMin(  p_label : String  ) : void {
			(_ChannelArray[p_label][1] as Channel).$VolumeToMin();
		}
		
		public function $SetVolume(  p_label : String , p_vol : Number, p_progresive : Boolean = false ) : void {
			var sTransform:SoundTransform = (_ChannelArray[p_label][1] as Channel).$SChannel.soundTransform;
			if ( !p_progresive ) {
				sTransform.volume = p_vol; //Volume level is a Number between 0 and 1
				(_ChannelArray[p_label][1] as Channel).$SChannel.soundTransform = sTransform;
			} else {				
				(_ChannelArray[p_label][1] as Channel).$VolumeTo( p_vol );
			}
			
		}
		
		public function $GetVolume( p_label : String ) : Number {
			return (_ChannelArray[p_label][1] as Channel).$SChannel.soundTransform.volume;
		}
		
		public function $GetPan( p_label : String ) : Number {
			return (_ChannelArray[p_label][1] as Channel).$SChannel.soundTransform.pan;
		}
		
		public function $SetPan( p_label : String, p_pan:Number) : void {
			var sTransform:SoundTransform = (_ChannelArray[p_label][1] as Channel).$SChannel.soundTransform;
			sTransform.pan = p_pan; //Volume level is a Number between 0 and 1
			(_ChannelArray[p_label][1] as Channel).$SChannel.soundTransform = sTransform;
		}
		
		public function $ToogleSound() : void{
			
			$Mute = !$Mute;
			
			if ( $Mute ) {
				SoundMixer.soundTransform = new SoundTransform( 0 );
			} else {
				SoundMixer.soundTransform = new SoundTransform( 1 );
			}
			
		}
		
	}
	
}